// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
var Root = require("Root");

cc.Class({
    //extends: cc.Component,
    __ctor__: function (row, column, waste = 0) {
        this.type = Math.floor(Math.random() * 4);  //随机模板，0：原版，1：横向翻转，2：纵向翻转，3：对角线翻转
        //this.type =3;
        console.log("type = " + this.type);
        this.row = row;
        this.column = column;
        this.backGround = cc.find("Canvas/PuzzleBG");
        this.content = cc.find("Canvas/PuzzleChooseScrollView/view/content");
        this.main = cc.find("Canvas").getComponent("Main");
        this.maxWidth = 120 * column;
        this.maxHeight = 120 * row;
        this.waste = waste;     //废片数量

        //var self = this;
        //console.log("struct");
    },
    properties: {
        //行
        row: 0,
        //列
        column: 0,
        //当前拼图资源
        texture: {
            default: null,
            type: cc.Texture2D,
        },
        //当前原始图片数据
        originData: null,
        maxWidth: 0,
        maxHeight: 0,
        //遮罩图集
        spriteAtlas: {
            default: null,
            type: cc.SpriteAtlas,
        },
        //所有小块数组
        spriteArray: {
            default: null,
        },
        //废片数组
        useless: {
            default: null,
        },
        //排列类型
        type: {
            default: -1,  //初始-1, 0：不做翻转，1：左右翻转，2：上下翻转，3：对角线翻转
        },
        //背景板
        backGround: {
            default: null,
        },
        //一共多少块
        num: {
            get() {
                return this._bar;
            },
            set(value) {
                this._bar = value;
                //switch (this.type) {
                //case 0:
                this.bgoffset = 0;
                switch (this.row * this.column) {
                    case 35:
                        this.bgoffset = 0.5;
                        break;
                    case 24:
                        this.bgoffset = 1;
                        break;

                    default:
                        break;
                }
                this.spriteArray = new Array(this.row);
                for (let i = 0; i < this.spriteArray.length; i++) {
                    this.spriteArray[i] = new Array(this.column);
                    for (let j = 0; j < this.spriteArray[i].length; j++) {
                        let index = (i * this.column + j + 1).toString();       //图片名1-48
                        let name = (i + 1) + "_" + (j + 1);
                        let newNode = new cc.Node(index.toString());
                        newNode.width = this.maxWidth / this.column;
                        newNode.height = this.maxHeight / this.row;
                        let childNode = new cc.Node(index.toString());
                        let id = 0;
                        let spriteFrame = null;

                        // let pixelx = 0;
                        // let pixely = 0;
                        let childposx = 0;
                        let childposy = 0;
                        let offsetx = 0;
                        let offsety = 0;
                        let backposx = newNode.width * j + newNode.width / 2 + 120 * this.bgoffset;
                        let backposy = newNode.height * i + newNode.height / 2 + 120 * this.bgoffset;
                        // let backposx = 0;
                        // let backposy = 0;

                        switch (this.type) {
                            case 0:
                                id = this.num - i * this.column - (this.column - 1 - j);  //id对应图片名
                                //name = (i + 1) + "_" + (j + 1);
                                //maskNode = new cc.Node(index.toString());
                                spriteFrame = new cc.SpriteFrame(this.newTexture, cc.rect(this.vertex[this.row - i - 1][j].pos, 0, this.vertex[this.row - i - 1][j].width, this.vertex[this.row - i - 1][j].height), false, cc.v2(0, 0), cc.size(this.vertex[this.row - i - 1][j].width, this.vertex[this.row - i - 1][j].height));
                                childNode.width = this.vertex[this.row - i - 1][j].width;
                                childNode.height = this.vertex[this.row - i - 1][j].height;
                                //spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
                                // childNode.width = spriteFrame.getOriginalSize().width;
                                // childNode.height = spriteFrame.getOriginalSize().height;
                                newNode.setScale(1, 1);
                                // backposx = newNode.width * (this.row - i - 1) + newNode.width / 2;
                                // backposy = newNode.height * j + newNode.height / 2;
                                // pixelx = defaultList[this.num][this.row - i - 1][j].x;
                                // pixely = this.texture.height - defaultList[this.num][this.row - i - 1][j].y - childNode.height;
                                childposx = defaultList[this.num][this.row - 1 - i][j].x + childNode.width / 2 + 120 * this.bgoffset;
                                childposy = defaultList[this.num][this.row - 1 - i][j].y + childNode.height / 2 + 120 * this.bgoffset;
                                // childposx = backposx;
                                // childposy = backposy;
                                //console.log(childposx + "," + childposy);
                                // backposx = newNode.width * i + newNode.width / 2;
                                // backposy = newNode.height * j + newNode.height / 2;
                                break;
                            case 1:
                                id = this.num - i * this.column - j;  //id对应图片名
                                //name = (i + 1) + "_" + (j + 1);
                                spriteFrame = new cc.SpriteFrame(this.newTexture, cc.rect(this.vertex[this.row - i - 1][this.column - 1 - j].pos, 0, this.vertex[this.row - i - 1][this.column - 1 - j].width, this.vertex[this.row - i - 1][this.column - 1 - j].height), false, cc.v2(0, 0), cc.size(this.vertex[this.row - i - 1][this.column - 1 - j].width, this.vertex[this.row - i - 1][this.column - 1 - j].height));
                                childNode.width = this.vertex[this.row - i - 1][this.column - 1 - j].width;
                                childNode.height = this.vertex[this.row - i - 1][this.column - 1 - j].height;
                                // spriteFrame = new cc.SpriteFrame(this.newTexture, cc.rect(this.vertex[this.row-i-1][j].pos, 0, this.vertex[this.row-i-1][j].width, this.vertex[this.row-i-1][j].height), false, cc.v2(0, 0), cc.size(this.vertex[this.row-i-1][j].width, this.vertex[this.row-i-1][j].height));
                                // childNode.width = this.vertex[this.row-i-1][j].width;
                                // childNode.height = this.vertex[this.row-i-1][j].height;
                                //maskNode = new cc.Node(index.toString());
                                //spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
                                // childNode.width = spriteFrame.getOriginalSize().width;
                                // childNode.height = spriteFrame.getOriginalSize().height;
                                //childNode.setScale(-1, 1);
                                // backposx = newNode.width * (this.row - i - 1) + newNode.width / 2;
                                // backposy = newNode.height * (this.column - j - 1) + newNode.height / 2;
                                // pixelx = this.texture.width - defaultList[this.num][this.row - i - 1][this.column - j - 1].x - childNode.width;
                                // pixely = this.texture.height - defaultList[this.num][this.row - i - 1][this.column - j - 1].y - childNode.height;
                                childposx = this.maxWidth - defaultList[this.num][this.row - i - 1][this.column - j - 1].x - childNode.width / 2 + 120 * this.bgoffset;
                                childposy = defaultList[this.num][this.row - i - 1][this.column - j - 1].y + childNode.height / 2 + 120 * this.bgoffset;
                                break;
                            case 2:
                                id = i * this.column + j + 1;  //id对应图片名
                                //name = (i + 1) + "_" + (j + 1);
                                spriteFrame = new cc.SpriteFrame(this.newTexture, cc.rect(this.vertex[i][j].pos, 0, this.vertex[i][j].width, this.vertex[i][j].height), false, cc.v2(0, 0), cc.size(this.vertex[i][j].width, this.vertex[i][j].height));
                                childNode.width = this.vertex[i][j].width;
                                childNode.height = this.vertex[i][j].height;
                                //maskNode = new cc.Node(index.toString());
                                //spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
                                // childNode.width = spriteFrame.getOriginalSize().width;
                                // childNode.height = spriteFrame.getOriginalSize().height;
                                //childNode.setScale(1, -1);
                                // backposx = newNode.width * i + newNode.width / 2;
                                // backposy = newNode.height * j + newNode.height / 2;
                                // pixelx = defaultList[this.num][i][j].x;
                                // pixely = defaultList[this.num][i][j].y;
                                childposx = defaultList[this.num][i][j].x + childNode.width / 2 + 120 * this.bgoffset;
                                childposy = this.maxHeight - defaultList[this.num][i][j].y - childNode.height / 2 + 120 * this.bgoffset;
                                break;
                            case 3:
                                id = this.num - i * this.column - j;  //id对应图片名
                                //name = (this.row - i) + "_" + (j + 1);
                                spriteFrame = new cc.SpriteFrame(this.newTexture, cc.rect(this.vertex[i][this.column - 1 - j].pos, 0, this.vertex[i][this.column - 1 - j].width, this.vertex[i][this.column - 1 - j].height), false, cc.v2(0, 0), cc.size(this.vertex[i][this.column - 1 - j].width, this.vertex[i][this.column - 1 - j].height));
                                childNode.width = this.vertex[i][this.column - 1 - j].width;
                                childNode.height = this.vertex[i][this.column - 1 - j].height;
                                // spriteFrame = new cc.SpriteFrame(this.newTexture, cc.rect(this.vertex[this.row-i-1][j].pos, 0, this.vertex[this.row-i-1][j].width, this.vertex[this.row-i-1][j].height), false, cc.v2(0, 0), cc.size(this.vertex[this.row-i-1][j].width, this.vertex[this.row-i-1][j].height));;
                                // childNode.width = this.vertex[this.row-i-1][j].width;
                                // childNode.height = this.vertex[this.row-i-1][j].height;
                                //maskNode = new cc.Node(index.toString());
                                //spriteFrame = this.spriteAtlas.getSpriteFrame(id.toString());
                                // childNode.width = spriteFrame.getOriginalSize().width;
                                // childNode.height = spriteFrame.getOriginalSize().height;
                                //childNode.setScale(-1, -1);
                                // backposx = newNode.width * (this.row - i - 1) + newNode.width / 2;
                                // backposy = newNode.height * (this.column - j - 1) + newNode.height / 2;
                                // pixelx = this.texture.width - defaultList[this.num][this.row - i - 1][this.column - j - 1].x - childNode.width;
                                // pixely = defaultList[this.num][this.row - i - 1][this.column - j - 1].y;
                                childposx = this.maxWidth - defaultList[this.num][i][this.column - j - 1].x - childNode.width / 2 + 120 * this.bgoffset;
                                childposy = this.maxHeight - defaultList[this.num][i][this.column - j - 1].y - childNode.height / 2 + 120 * this.bgoffset;

                                break;
                            default:
                                break;
                        }


                        let backNode = cc.instantiate(newNode);; //背景位置节点
                        backNode.name = name;
                        //let childNode = cc.instantiate(maskNode);
                        newNode.addChild(childNode);
                        backNode.setParent(this.backGround);
                        backNode.setPosition(backposx - 360, backposy + 40);
                        //backNode.addComponent(cc.Sprite).spriteFrame = spriteFrame;
                        // let mask = maskNode.addComponent(cc.Mask);
                        // mask.type = cc.Mask.Type.IMAGE_STENCIL;
                        // mask.spriteFrame = spriteFrame;
                        // mask.alphaThreshold = 0.5;
                        // //mask.resize();
                        // maskNode.addChild(childNode);
                        offsetx = childposx - backposx;
                        offsety = childposy - backposy;
                        //console.log(offsetx + "," + offsety);
                        //backNode.setPosition(backNode.position.x-offsetx, backNode.position.y - offsety);

                        // console.log("差值");
                        // console.log(offsetx + "," + offsety);
                        childNode.setPosition(offsetx, offsety);
                        let sprite = childNode.addComponent(cc.Sprite);
                        // console.log(pixelx + "," + pixely);
                        // console.log(childNode.width + "," + childNode.height);
                        //let frame = new cc.SpriteFrame(this.texture, cc.rect(pixelx, pixely, childNode.width, childNode.height), false, cc.v2(0, 0), cc.size(childNode.width, childNode.height));
                        sprite.spriteFrame = spriteFrame;
                        //console.log(frame);
                        this.spriteArray[i][j] = { name: index, row: i + 1, column: j + 1, node: newNode };

                        this.spriteArray[i][j].node.setParent(this.content);
                        let drag = this.spriteArray[i][j].node.addComponent("Drag");
                        // drag.width = i + 1;
                        // drag.height = j + 1;
                        // console.log(name);
                        drag.name = name;
                        //this.spriteArray[i][j].node.position = cc.v2(index * 200, 20);
                        this.main.startpuzzle.push(this.spriteArray[i][j].node);
                        // this.spriteArray[i][j].node.setParent(this.backGround);
                        // this.spriteArray[i][j].node.position = backNode.position;
                        //   console.log(backNode.name);
                    }
                }

            }
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
        // var index = Math.floor(Math.random() * 4) + 1;
        // var self = this;
        // cc.loader.loadRes("background/img" + index, cc.Texture2D, function (err, texture) {
        //     self.texture = texture;
        //     console.log(texture.getPixelFormat());
        //     console.log(texture);
        //     cc.loader.loadRes("Atlas/atlas" + self.row * self.column, cc.SpriteAtlas, function (err, atlas) {
        //         console.log(atlas);
        //         self.spriteAtlas = atlas;
        //         self.num = self.row * self.column;
        //         self.generateUseless(self.column);
        //         self.randomSortAndRotate();
        //         self.addBGPuzzle();
        //         console.log(self.main.startpuzzle);
        //     });
        // });

        //var index = Math.floor(Math.random() * 2) + 1;
        // var index = 3;
        var self = this;
        console.log(cc.find("root").getComponent(Root).index);
        self.ChoosePuzzle(cc.find("root").getComponent(Root).kindPuzzle, cc.find("root").getComponent(Root).index, cc.find("root").getComponent(Root).isRotate);
        // cc.loader.loadRes("Atlas/atlas_" + (this.row * this.column) + "_" + index, cc.SpriteAtlas, function (err, atlas) {
        //     // self.texture = texture;
        //     // console.log(texture.getPixelFormat());
        //     // console.log(texture);
        //     //cc.loader.loadRes("Atlas/atlas" + self.row * self.column, cc.SpriteAtlas, function (err, atlas) {
        //     //console.log(atlas);
        //     self.spriteAtlas = atlas;
        //     self.num = self.row * self.column;
        //     self.generateUseless(self.column);
        //     self.randomSortAndRotate();
        //     self.addBGPuzzle();
        //     // console.log(self.main.startpuzzle);
        //     //});
        // });

    },
    //2019-1-8 开始游戏 新加功能
    startLevel(tex) {
        console.log("开始游戏");
        this.texture = tex;
        let renderTexture = new cc.RenderTexture();
        renderTexture.initWithSize(this.texture.width, this.texture.height);
        renderTexture.drawTextureAt(this.texture, 0, 0);
        this.originData = renderTexture.readPixels();
        this.getNewTexture();
        this.main.completePic.addComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(tex);
        this.main.completePic.active = false;
        this.num = this.row * this.column;
        console.log("几个废片：" + this.waste)
        this.generateUseless(this.waste);
        this.randomSortAndRotate(cc.find("root").getComponent(Root).isRotate);
        this.addBGPuzzle();
    },
    //获取新材质 2019-1-8 新加功能
    getNewTexture() {
        this.dataArray = [];
        this.newTextureData = null;
        let allWidth = 0;
        let pieces;
        this.vertex = [];       //顶点坐标
        this.wasteVertex = [];  //废片顶点坐标
        if (this.row * this.column == 48) {
            pieces = cc.piece48;
        }
        else if (this.row * this.column == 35) {

        }
        else if (this.row * this.column == 24) {
            pieces = cc.piece24;
        }
        for (let i = 0; i < this.row; i++) {
            this.vertex[i] = new Array(this.column);
            for (let j = 0; j < this.column; j++) {
                let name = j + i * this.column + 1;
                //cc.loader.loadRes(name.toString(), cc.Texture2D, function (err, texttttt) {
                let thisTex = pieces[name];
                //console.log(thisTex);
                // let node = this.showSprite(name, tex, i * 100, j * 100);
                // node.parent = cc.find("Canvas");
                // node.setPosition(i * 200 - 360, j * 200 - 480, 0);
                let pixelx = 0;
                let pixely = 0;
                switch (this.type) {
                    case 0:
                        pixelx = defaultList[this.row * this.column][i][j].x;
                        pixely = this.texture.height - defaultList[this.row * this.column][i][j].y - thisTex.height;
                        //pixely = defaultList[this.row * this.column][this.row - i - 1][j].y;

                        break;
                    case 1:
                        pixelx = this.texture.width - defaultList[this.row * this.column][i][j].x - thisTex.width;
                        pixely = this.texture.height - defaultList[this.row * this.column][i][j].y - thisTex.height;
                        break;
                    case 2:
                        pixelx = defaultList[this.row * this.column][i][j].x;
                        pixely = defaultList[this.row * this.column][i][j].y;
                        break;
                    case 3:
                        pixelx = this.texture.width - defaultList[this.row * this.column][i][j].x - thisTex.width;
                        pixely = defaultList[this.row * this.column][i][j].y;
                        break;
                    default:
                        break;
                }
                // pixelx = defaultList[this.row * this.column][i][j].x;
                // pixely = this.texture.height - defaultList[this.row * this.column][i][j].y - thisTex.height;
                this.dataArray.push({ width: thisTex.width, height: thisTex.height, data: this.getTextureData(thisTex, pixelx, pixely, this.type) });
                this.vertex[i][j] = { pos: allWidth, width: thisTex.width, height: thisTex.height };
                allWidth += thisTex.width + 2;
                //console.log(this.getTextureData(tex, i * 100, j * 100));

                //}.bind(this));

            }
        }
        for (let i = 0; i < this.waste; i++) {
            let name = Math.floor(Math.random() * this.row * this.column) + 1;
            //cc.loader.loadRes(name.toString(), cc.Texture2D, function (err, texttttt) {
            let thisTex = pieces[name];
            console.log(thisTex);
            // let node = this.showSprite(name, tex, i * 100, j * 100);
            // node.parent = cc.find("Canvas");
            // node.setPosition(i * 200 - 360, j * 200 - 480, 0);
            let pixelx = Math.floor(Math.random() * 120 * (this.column - 2));
            let pixely = Math.floor(Math.random() * 120 * (this.row - 2));
            // let x = (name - 1) % this.column;
            // let y = Math.floor((name - 1) / this.column);
            // switch (this.type) {
            //     case 0:
            //         pixelx = defaultList[this.row * this.column][i][j].x;
            //         pixely = this.texture.height - defaultList[this.row * this.column][i][j].y - thisTex.height;
            //         //pixely = defaultList[this.row * this.column][this.row - i - 1][j].y;

            //         break;
            //     case 1:
            //         pixelx = this.texture.width - defaultList[this.row * this.column][i][j].x - thisTex.width;
            //         pixely = this.texture.height - defaultList[this.row * this.column][i][j].y - thisTex.height;
            //         break;
            //     case 2:
            //         pixelx = defaultList[this.row * this.column][i][j].x;
            //         pixely = defaultList[this.row * this.column][i][j].y;
            //         break;
            //     case 3:
            //         pixelx = this.texture.width - defaultList[this.row * this.column][i][j].x - thisTex.width;
            //         pixely = defaultList[this.row * this.column][i][j].y;
            //         break;
            //     default:
            //         break;
            // }
            // pixelx = defaultList[this.row * this.column][i][j].x;
            // pixely = this.texture.height - defaultList[this.row * this.column][i][j].y - thisTex.height;
            this.dataArray.push({ width: thisTex.width, height: thisTex.height, data: this.getTextureData(thisTex, pixelx, pixely, this.type) });
            this.wasteVertex[i] = { pos: allWidth, width: thisTex.width, height: thisTex.height, id: name };
            allWidth += thisTex.width + 2;
        }
        if (true) {
            this.newTextureData = this.makeNewBGTexture(this.dataArray);
            console.log(this.newTextureData);
            this.newTexture = new cc.RenderTexture();
            let isDone = this.newTexture.initWithData(this.newTextureData, cc.Texture2D.PixelFormat.RGBA8888, allWidth, 200);
            // console.log(isDone);
            console.log(this.newTexture);
            if (!isDone) {
                return null;
            }
            // else {
            //     return this.newTexture;
            // }
            // let node = new cc.Node(name);
            // let sp = node.addComponent(cc.Sprite);
            // //sp.spriteFrame = new cc.SpriteFrame(newTexture, cc.rect(allWidth - thisTex.width, 0, thisTex.width, 200), false, cc.v2(0, 0), cc.size(thisTex.width, 200));
            // sp.spriteFrame = new cc.SpriteFrame(this.newTexture, cc.rect(0, 0, allWidth, 200), false, cc.v2(0, 0), cc.size(allWidth, 200));

            // console.log(sp.spriteFrame);
            // node.parent = cc.find("Canvas");
            // node.on(cc.Node.EventType.TOUCH_MOVE, function(event){
            //     console.log(node.name);
            //     let delta = event.getDelta();
            //     node.x += delta.x;
            //     node.y += delta.y;
            // }.bind(this));
        }
    },
    /**
     * 获得小块模板数据 2019-1-8 新加功能
     * @param {cc.Texture2D} texture 要读取的图片
     * @param {Number} x 读取位置x坐标
     * @param {Number} y 读取位置y坐标
     * @returns {Uint8Array} 图片像素数据
     */
    getTextureData(texture, x, y, type = 0) {
        //data = [];
        let rt = new cc.RenderTexture();
        rt.initWithSize(texture.width, texture.height);
        rt.drawTextureAt(texture, 0, 0);
        // data就是这个texture的rgba值数组
        let data = rt.readPixels();
        let pixelFormat = rt.getPixelFormat();
        let newArray = [];
        if (type == 0) {
            newArray = data;
        } else if (type == 1) {
            for (let i = 0; i < texture.height; i++) {
                for (let j = texture.width - 1; j >= 0; j--) {
                    for (let k = 0; k < 4; k++) {
                        newArray.push(data[(i * texture.width + j) * 4 + k]);
                    }
                }
            }
        } else if (type == 2) {
            for (let i = texture.height - 1; i >= 0; i--) {
                for (let j = 0; j < texture.width; j++) {
                    for (let k = 0; k < 4; k++) {
                        newArray.push(data[(i * texture.width + j) * 4 + k]);
                    }
                }
            }
        } else if (type == 3) {
            for (let i = texture.height - 1; i >= 0; i--) {
                for (let j = texture.width - 1; j >= 0; j--) {
                    for (let k = 0; k < 4; k++) {
                        newArray.push(data[(i * texture.width + j) * 4 + k]);
                    }
                }
            }
        }
        rt.destroy();
        let indexArray = [];
        //按行排列
        for (let i = y; i < y + texture.height; i++) {
            for (let j = x; j < x + texture.width; j++) {
                indexArray.push(j + i * this.texture.width);
            }
        }
        //按列排序
        // for(let i = x; i < x + this.texture.width; i++) {
        //     for(let j = y; j < y + this.texture.height; j++) {
        //         indexArray.push(i + j * this.bg.height);
        //     }
        // }


        /////////////////////////////////////////////////////////////
        //console.log(indexArray);
        let rectArray = [];
        for (let i = 0; i < indexArray.length; i++) {
            for (let j = 0; j < 4; j++) {
                rectArray.push(this.originData[indexArray[i] * 4 + j]);
            }
        }
        //rectArray = new Uint8Array(rectArray);
        //console.log(rectArray);
        for (let i = 0; i < newArray.length; i++) {
            if ((i + 1) % 4 == 0) {
                //console.log(i);
                rectArray[i] = newArray[i];
            }

        }
        for (let i = newArray.length; i < texture.width * 200 * 4; i++) {
            rectArray.push(0);
        }

        return new Uint8Array(rectArray);
    },
    /**
     * 制作整图 2019-1-8 新加功能
     * @param {Array} dataArray 图片数据
     */
    makeNewBGTexture(dataArray) {
        let newData = [];
        for (let i = 0; i < 200; i++) {
            for (let j = 0; j < dataArray.length; j++) {
                for (let k = 0; k < dataArray[j].width; k++) {
                    for (let l = 0; l < 4; l++) {
                        newData.push(dataArray[j].data[(i * dataArray[j].width + k) * 4 + l]);
                    }
                }
                //间隔2像素，避免切到相邻图
                for (let m = 0; m < 8; m++) {
                    newData.push(0);
                }
            }
        }
        console.log(newData);
        return new Uint8Array(newData);
    },
    ChoosePuzzle(kindName, index, isRotate) {

        var self = this;
        console.log(kindName);
        console.log("Atlas/" + kindName + "/atlas_" + kindName + "_" + index);
        cc.loader.loadRes("Atlas/" + kindName + "/atlas_" + kindName + "_" + index, cc.SpriteAtlas, function (err, atlas) {
            // self.texture = texture;
            // console.log(texture.getPixelFormat());
            // console.log(texture);
            //cc.loader.loadRes("Atlas/atlas" + self.row * self.column, cc.SpriteAtlas, function (err, atlas) {
            console.log(atlas);
            self.spriteAtlas = atlas;
            self.num = self.row * self.column;
            self.generateUseless(self.column);
            self.randomSortAndRotate(isRotate);
            self.addBGPuzzle();
            // console.log(self.main.startpuzzle);
            //});
        });
        cc.loader.loadRes("CompletePic/" + kindName + "/" + index + "Pic", cc.SpriteFrame, function (err, sp) {
            console.log(self.main.completePic);
            self.main.completePic.addComponent(cc.Sprite).spriteFrame = sp;
            console.log(self.main.completePic);
            self.main.completePic.active = false;
            // self.main.completePic.parent = self.main.lockPart;
            // self.main.completePic.position = cc.v2(self.main.completePic.position.x, self.main.completePic.position.y + 520);

            console.log(self.main.completePic);

        });
    },

    // update (dt) {},

    //生成废片
    generateUseless(num) {
        //console.log(num);
        this.useless = new Array(num);
        for (let i = 0; i < num; i++) {
            //let id = Math.floor(Math.random() * this.num) + 1;  //id对应图片名
            let index = (this.num + i + 1).toString();       //图片名
            let newNode = new cc.Node(index.toString());
            let name = "0_0";
            newNode.width = this.maxWidth / this.column;
            newNode.height = this.maxHeight / this.row;
            let childNode = new cc.Node(index.toString());
            let spriteFrame = null;
            
            spriteFrame = new cc.SpriteFrame(this.newTexture, cc.rect(this.wasteVertex[i].pos, 0, this.wasteVertex[i].width, this.wasteVertex[i].height), false, cc.v2(0, 0), cc.size(this.wasteVertex[i].width, this.wasteVertex[i].height));
            childNode.width = this.wasteVertex[i].width;
            childNode.height = this.wasteVertex[i].height;
            let id = this.wasteVertex[i].id;
            let x;
            let y;
            
            
            let childposx;
            let childposy;
            switch (this.type) {
                case 0:
                y = (id - 1) % this.column;
                x = this.row - Math.floor((id - 1) / this.column) - 1;
                childposx = defaultList[this.num][this.row - 1 - x][y].x + childNode.width / 2 + 120 * this.bgoffset;
                childposy = defaultList[this.num][this.row - 1 - x][y].y + childNode.height / 2 + 120 * this.bgoffset;
                    break;
                case 1:
                y = this.column - (id - 1) % this.column - 1;
                x = this.row - Math.floor((id - 1) / this.column) - 1;
                childposx = this.maxWidth - defaultList[this.num][this.row - x - 1][this.column - 1 - y].x - childNode.width / 2 + 120 * this.bgoffset;
                //childposx = defaultList[this.num][this.row - x - 1][this.column - 1 - y].x + childNode.width / 2 + 120 * this.bgoffset;

                childposy = defaultList[this.num][this.row - x - 1][this.column - 1 - y].y + childNode.height / 2 + 120 * this.bgoffset;
                    break;
                case 2:
                y = (id - 1) % this.column;
                x = Math.floor((id - 1) / this.column);
                childposx = defaultList[this.num][x][y].x + childNode.width / 2 + 120 * this.bgoffset;
                childposy = this.maxHeight - defaultList[this.num][x][y].y - childNode.height / 2 + 120 * this.bgoffset;
                    break;
                case 3:
                y = this.column - (id - 1) % this.column - 1;
                x = Math.floor((id - 1) / this.column);
                childposx = this.maxWidth - defaultList[this.num][x][this.column - 1 - y].x - childNode.width / 2 + 120 * this.bgoffset;
                childposy = this.maxHeight - defaultList[this.num][x][this.column - 1 - y].y - childNode.height / 2 + 120 * this.bgoffset;
                    break;
                default:
                    break;
            }
            console.log("id=" + id);
            console.log(x + "," + y);
            let backposx = newNode.width * y + newNode.width / 2 + 120 * this.bgoffset;
            let backposy = newNode.height * x + newNode.height / 2 + 120 * this.bgoffset;
            // let spriteFrame = this.spriteAtlas.getSpriteFrame(index.toString());
            // childNode.width = spriteFrame.getOriginalSize().width;
            // childNode.height = spriteFrame.getOriginalSize().height;
            //let childNode = cc.instantiate(maskNode);
            //newNode.addChild(maskNode);
            //let backNode = cc.instantiate(newNode); //背景位置节点
            //backNode.setParent(this.backGround);
            //backNode.setPosition(defaultList[this.num][this.row - i - 1][j].x + newNode.width / 2, defaultList[this.num][this.row - i - 1][j].y + newNode.height / 2);
            // let mask = maskNode.addComponent(cc.Mask);
            // mask.type = cc.Mask.Type.IMAGE_STENCIL;
            // mask.spriteFrame = spriteFrame;
            // mask.alphaThreshold = 0.5;
            //mask.resize();
            newNode.addChild(childNode);
            console.log(childposx + "," + childposy);
            console.log(backposx + "," + backposy);
            console.log((childposx - backposx) + "," + (childposy - backposy));

            childNode.setPosition(childposx - backposx, childposy - backposy);
            let sprite = childNode.addComponent(cc.Sprite);
            // let posx = Math.floor(Math.random() * 320) + 200;
            // let posy = Math.floor(Math.random() * 560) + 200;
            //let frame = new cc.SpriteFrame(this.texture, cc.rect(posx, posy, maskNode.width, maskNode.height), false, cc.v2(0, 0), cc.size(maskNode.width, maskNode.height));
            sprite.spriteFrame = spriteFrame;
            this.useless[i] = { name: index.toString(), row: -1, column: -1, node: newNode };
            this.useless[i].node.setParent(this.content);
            let drag = this.useless[i].node.addComponent("Drag");
            // drag.width = -1;
            // drag.height = -1;
            drag.name = name;
            //this.useless[i].node.position = cc.v2((i + 48) * 200, 20);
            //this.useless[i].node.position = cc.v2(i * 200, 20);
            this.main.startpuzzle.push(this.useless[i].node);
        }
    },
    //随机排序、随机旋转和定位置
    randomSortAndRotate(isRotate) {
        var newArray = [];
        for (let i = 0; i < this.row * this.column + this.waste; i++) {
            let ran = 0;//Math.floor(Math.random() * this.main.startpuzzle.length);
            let ranrot = Math.floor(Math.random() * 4) * 90;
            newArray.push(this.main.startpuzzle[ran]);
            this.main.startpuzzle.splice(ran, 1);
            //newArray[i].position = cc.v2(100 + i * 200, 20);   
            // console.log(this.main.startpuzzle);
            // console.log(newArray);
            // console.log(newArray[i]);
            //let drag = newArray[i].getComponent("Drag");
            // console.log(drag);
            //console.log(this.main.startpos);
            // console.log(newArray[i]);
            //newArray[i].setParent(this.content);
            newArray[i].position = cc.v2(this.main.startpos[i].x, this.main.startpos[i].y);
            if (isRotate == true) {
                newArray[i].rotation = ranrot;
            }
            else {
                newArray[i].rotation = 0;

            }
        }

        this.content.position = cc.v2(- 360, this.content.position.y);
        this.main.startpuzzle = newArray;
    },
    //添加背景块数组
    addBGPuzzle() {
        let bgchildren = cc.find("Canvas/PuzzleBG").getChildren();
        //console.log(bgchildren);
        for (let i = 0; i < bgchildren.length; i++) {
            this.main.puzzlebgChildren.push(bgchildren[i]);
        }
        //console.log(this.main.puzzlebgChildren);
        for (let i = 0; i < this.row; i++) {
            this.main.puzzleBG2.push([]);
            for (let j = 0; j < this.column; j++) {
                this.main.puzzleBG2[i].push(this.main.puzzlebgChildren[j]);

            }
            this.main.puzzlebgChildren.splice(0, this.column);
        }
        //console.log(this.main.puzzleBG2);
        for (let j = 0; j < this.row * this.column + this.waste; j++) {
            let drag = this.main.startpuzzle[j].getComponent("Drag");
            drag.startpuzzle = this.main.startpuzzle;
            drag.puzzleBG2 = this.main.puzzleBG2;
            drag.isLock = this.main.isLock;
            //   console.log(drag.puzzleBG2);

            for (let i = 0; i < bgchildren.length; i++) {
                drag.puzzleBG.push(bgchildren[i]);
            }
        }


    },
});

var defaultList = {
    48: [
        [
            { x: 0, y: 807 },
            { x: 112, y: 835 },
            { x: 210, y: 811 },
            { x: 352, y: 839 },
            { x: 473, y: 808 },
            { x: 567, y: 829 },
        ],
        [
            { x: 0, y: 711 },
            { x: 87, y: 711 },
            { x: 206, y: 713 },
            { x: 331, y: 717 },
            { x: 469, y: 687 },
            { x: 589, y: 684 },
        ],
        [
            { x: 0, y: 593 },
            { x: 91, y: 592 },
            { x: 231, y: 593 },
            { x: 362, y: 594 },
            { x: 470, y: 592 },
            { x: 588, y: 595 },
        ],
        [
            { x: 0, y: 450 },
            { x: 89, y: 443 },
            { x: 237, y: 475 },
            { x: 329, y: 471 },
            { x: 477, y: 451 },
            { x: 565, y: 455 },
        ],
        [
            { x: 0, y: 361 },
            { x: 110, y: 353 },
            { x: 208, y: 329 },
            { x: 351, y: 337 },
            { x: 475, y: 357 },
            { x: 565, y: 353 },
        ],
        [
            { x: 0, y: 211 },
            { x: 88, y: 239 },
            { x: 207, y: 232 },
            { x: 358, y: 204 },
            { x: 445, y: 230 },
            { x: 589, y: 204 },
        ],
        [
            { x: 0, y: 91 },
            { x: 118, y: 120 },
            { x: 232, y: 98 },
            { x: 331, y: 92 },
            { x: 473, y: 118 },
            { x: 571, y: 118 },
        ],
        [
            { x: 0, y: 0 },
            { x: 86, y: 0 },
            { x: 236, y: 0 },
            { x: 351, y: 0 },
            { x: 445, y: 0 },
            { x: 595, y: 0 },
        ],
    ],
    35: [],
    24: [
        [
            { x: 0, y: 807 - 240 },
            { x: 112, y: 835 - 240 },
            { x: 210, y: 811 - 240 },
            { x: 352, y: 839 - 240 },
            //{ x: 473, y: 808 },
            //{ x: 567, y: 829 },
        ],
        [
            { x: 0, y: 711 - 240 },
            { x: 87, y: 711 - 240 },
            { x: 206, y: 713 - 240 },
            { x: 331, y: 717 - 240 },
            //{ x: 469, y: 687 },
            //{ x: 589, y: 684 },
        ],
        [
            { x: 0, y: 593 - 240 },
            { x: 91, y: 592 - 240 },
            { x: 231, y: 593 - 240 },
            { x: 362, y: 594 - 240 },
            //{ x: 470, y: 592 },
            //{ x: 588, y: 595 },
        ],
        [
            { x: 0, y: 450 - 240 },
            { x: 89, y: 443 - 240 },
            { x: 237, y: 475 - 240 },
            { x: 329, y: 471 - 240 },
            //{ x: 477, y: 451 },
            //{ x: 565, y: 455 },
        ],
        [
            { x: 0, y: 361 - 240 },
            { x: 110, y: 353 - 240 },
            { x: 208, y: 329 - 240 },
            { x: 351, y: 337 - 240 },
            //{ x: 475, y: 357 },
            //{ x: 565, y: 353 },
        ],
        [
            { x: 0, y: 0 },
            { x: 88, y: 0 },
            { x: 207, y: 0 },
            { x: 358, y: 0 },
            //{ x: 445, y: 230 },
            //{ x: 589, y: 204 },
        ],
        // [
        //     { x: 0, y: 91 },
        //     { x: 118, y: 120 },
        //     { x: 232, y: 98 },
        //     { x: 331, y: 92 },
        //     //{ x: 473, y: 118 },
        //     //{ x: 571, y: 118 },
        // ],
        // [
        //     { x: 0, y: 0 },
        //     { x: 86, y: 0 },
        //     { x: 236, y: 0 },
        //     { x: 351, y: 0 },
        //     //{ x: 445, y: 0 },
        //     //{ x: 595, y: 0 },
        // ],
    ],
}
